﻿using System;
using System.Runtime.InteropServices;

namespace RxjhServer
{
    public class duibi
    {

        public static byte[] GetBytes(int value)
        {
            return new byte[]
            {
                (byte)(value & 255),
                (byte)(value >> 8 & 255),
                (byte)(value >> 16 & 255),
                (byte)(value >> 24 & 255)
            };
        }

        public static byte[] GetBytes(long value)
        {
            return new byte[]
            {
                (byte)(value & 255L),
                (byte)(value >> 8 & 255L),
                (byte)(value >> 16 & 255L),
                (byte)(value >> 24 & 255L),
                (byte)(value >> 32 & 255L),
                (byte)(value >> 40 & 255L),
                (byte)(value >> 48 & 255L),
                (byte)(value >> 56 & 255L)
            };
        }

        public static int ToInt32(byte[] src, int offset)
        {
            return (int)(src[offset] & 255) | (int)(src[offset + 1] & 255) << 8 | (int)(src[offset + 2] & 255) << 16 | (int)(src[offset + 3] & 255) << 24;
        }

        public static short ToInt16(byte[] src, int offset)
        {
            return (short)((int)src[offset] | (int)src[offset + 1] << 8);
        }

        public unsafe static bool IsEquals(string str1, string str2)
        {
            if (str1.Length != str2.Length)
            {
                return false;
            }
            bool result = true;
            int num = (str1.Length % 4 != 0) ? (4 - str1.Length % 4) : 0;
            int num2 = (str2.Length % 4 != 0) ? (4 - str1.Length % 4) : 0;
            int num3 = (num > num2) ? num : num2;
            int i;
            for (i = 0; i < num3; i++)
            {
                if (i < num)
                {
                    str1 += " ";
                }
                if (i < num2)
                {
                    str2 += " ";
                }
            }
            fixed (char* ptr = str1, ptr2 = str2)
            {
                char* ptr3 = ptr;
                char* ptr4 = ptr2;
                while (i < str1.Length)
                {
                    if (*(long*)ptr3 != *(long*)ptr4)
                    {
                        result = false;
                        break;
                    }
                    i += 4;
                    ptr3 += 4;
                    ptr4 += 4;
                }
            }
            return result;
        }

    }
}
